package novel.impl;

import novel.enums.NovelSpiderEnum;
import novel.util.NovelSpiderHttpGet;
import novel.util.NovelSpiderUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

/**
 * @Author:xdw
 * @Date:Created in 下午 1:402019/3/11 0011
 * @described:
 * @Modified By:
 */
public abstract class AbstractCrawlSpider {

    /**
     * 通过jsoup抓取页面内容
     *
     * @param url
     * @return
     * @throws Exception
     */
    protected String crawl(String url) throws Exception {
        try (CloseableHttpClient httpClient = HttpClientBuilder.create().build();
             CloseableHttpResponse httpResponse = httpClient.execute(new NovelSpiderHttpGet(url))) {
            String result = EntityUtils.toString(httpResponse.getEntity(),
                    NovelSpiderUtil.getContext(NovelSpiderEnum.getEnumByUrl(url)).get("charset"));
            return result;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
